import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybaseplus/base/tool/Animated/FFListenGroup.dart';
import 'package:mybaseplus/base/tool/calcuate/FFCalculateText.dart';
import 'package:mybaseplus/base/tool/globalMethod/FFScreenValue.dart';
import 'package:mybaseplus/base/tool/visibility/FFVisibility.dart';
import 'package:mybaseplus/base/widget/layout/FFImageButton.dart';
import 'package:mybaseplus/base/widget/layout/FFStackContainer.dart';
import 'package:mybaseplus_example/config/ConfigContext.dart';
import 'package:mybaseplus_example/pages/other/led/LEDWidget.dart';

class LEDItem extends StatefulWidget {
  String name;
  LEDWidgetParam param;
  ILEDItemDelegate? delegate;

  LEDItem({
    required this.name,
    Key? key,
    required this.param,
    this.delegate,
  }) : super(key: key);

  @override
  State<LEDItem> createState() => _LEDItemState();
}

class _LEDItemState extends State<LEDItem> with TickerProviderStateMixin {
  late FFListenGroup<double> group;

  @override
  void dispose() {
    // FFLog.info("删除了一个LEDItem");
    group.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    if (widget.param.type == LEDType.text) {
      goTextLED();
    } else if (widget.param.type == LEDType.img) {
      goImgLED();
    }

  }


  ///计算图片宽度，并开启动画
  goImgLED(){
    goTextLED();
  }

  ///计算文本宽度，并开启动画
  goTextLED() {
    if (!mounted) {
      return;
    }
    group = FFListenGroup(listen: FFListen(), valueListenable: ValueNotifier(0));
    setState(() {});

    Size size =
        FFCalculateText.calculateCharSize(widget.param.value, widget.param.textStyle!);
    double textLength = size.width + globalScreenWidth(getContext());
    int mill = textLength ~/ widget.param.speed! * 2;

    group.start(
        vsync: this, begin: 0, end: textLength, duration: Duration(milliseconds: mill));
  }

  @override
  Widget build(BuildContext context) {
    return group.uiListen2(builder: (BuildContext ctx, double value, Widget? child) {
      return Positioned(
        left: globalScreenWidth(context) - value,
        top: 0,
        bottom: 0,
        child: FFVisibility(
          onDisappear: () async {
            if (!mounted) {
              return;
            }
            // FFLog.info("${widget.name} LED消失");
            ///当该组件消失时，则会通知外层移除自己
            widget.delegate?.delegateDeleteCurrentLED(widget);
          },
          onAppear: () {
            if (!mounted) {
              return;
            }
            // FFLog.info("${widget.name} LED出现");
          },
          child: Container(
            // color: Colors.yellow,
            alignment: Alignment.center,
            child: FFStackContainer(
              children: [
                ///LED文本
                if (widget.param.type == LEDType.text)
                Text(
                  widget.param.value,
                  textScaleFactor: 1.0,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: widget.param.textStyle,
                ),

                if (widget.param.type == LEDType.img)
                FFImageButton(
                  child: FFImageFactory.asset(widget.param.value),
                ),

                Positioned(
                  right: 0,
                  child: FFVisibility(
                    onAppear: () async {
                      if (!mounted) {
                        return;
                      }
                      // FFLog.info("${widget.name} LED标记出现");
                      ///当该组件出现时，则会通知创建下一个LED
                      await Future.delayed(Duration(
                          milliseconds: ((1 - widget.param.speed!) * 1100).toInt()));
                      widget.delegate?.delegateCreateNextLED();
                    },
                    onDisappear: () {
                      if (!mounted) {
                        return;
                      }
                      // FFLog.info("${widget.name} LED标记消失");
                    },
                    child: Container(
                      width: 40,
                      height: 40,
                      color: Colors.transparent,
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      );
    });
  }
}

abstract class ILEDItemDelegate {
  delegateCreateNextLED();

  delegateDeleteCurrentLED(LEDItem item);
}
